package servlet;

import Hadoop.HadoopUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@WebServlet("/LoadServlet")
public class Load extends HttpServlet {

    public void init(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        init(req,resp);
        String display="";
        String srcPath=req.getParameter("srcPath");
        String destPath=req.getParameter("destPath");
        System.out.println(srcPath);
        String srcLoc=req.getParameter("srcLoc");
        String destLoc=req.getParameter("destLoc");
        HadoopUtil Url = new HadoopUtil();
        if(srcPath!=""&&destPath!="") {
            display = Url.hadoopUpload(srcPath, destPath);
        }else if(srcLoc!=""&&destLoc!=""){
            display = Url.hadoopDownload(destLoc,srcLoc);
        }
        resp.setContentType("text/html;charset=utf-8");
        System.out.println(display);
        HttpSession session = req.getSession();

        req.setAttribute("obj_data",display);

        req.getRequestDispatcher("/load.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
